﻿using System;
using System.Windows.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VideoReflector.Converters
{
    /// <summary>
    /// Converter taking a boolean value as an input and returning either of the properties
    /// TrueValue or FalseValue depending on whether the value is true or false respectively
    /// </summary>
    [ValueConversion(typeof(bool), typeof(object))]
    public class BooleanValueChooserConverter : ConverterWithMarkupExtension, IValueConverter
    {
        private object _trueValue;
        private object _falseValue;

        /// <summary>
        /// Object to return when input value is true
        /// </summary>
        public object TrueValue
        {
            get { return _trueValue;  }
            set { _trueValue = value; }
        }

        /// <summary>
        /// Object to return when input value is false
        /// </summary>
        public object FalseValue
        {
            get { return _falseValue;  }
            set { _falseValue = value; }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() != typeof(bool))
            {
                throw new ArgumentException("value passed to Convert method of BooleanValueChooserConverter must be of bool type");
            }
            else if ( TrueValue == null || FalseValue == null)
            {
                throw new Exception("TrueValue and FalseValue properties must be set before using converter");
            }
            var boolValue = System.Convert.ToBoolean(value);
            return boolValue ? TrueValue : FalseValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
